﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace MidTest.Library
{
    public class Task
    {
        public string Encrypt(string input, int steps)
        {
            string result = string.Empty;

            if (!isAlphaNumeric(input))
            {
                throw new Exception(input + " is not an alphanumeric string.");
            }
            else if (steps<1 || steps>5)
            {
                throw new Exception("Steps must be 1, 2, 3, 4, or 5.");
            }
            else
            {
                foreach (char c in input.ToCharArray())
                {
                    char temp = c;

                    for (int i = 0; i < steps; i++)
                    {
                        switch (temp)
                        {
                            case 'Z':
                                temp = 'A';
                                break;
                            case 'z':
                                temp = 'a';
                                break;
                            case '9':
                                temp = '0';
                                break;
                            default:
                                temp++;
                                break;
                        }
                    }

                    result += temp;
                }
            }
            
            return result;
        }

        public string Decrypt(string input, int steps)
        {
            string result = string.Empty;

            if (!isAlphaNumeric(input))
            {
                throw new Exception(input + " is not an alphanumeric string.");
            }
            else if (steps < 1 || steps > 5)
            {
                throw new Exception("Steps must be 1, 2, 3, 4, or 5.");
            }
            else
            {
                foreach (char c in input.ToCharArray())
                {
                    char temp = c;

                    for (int i = 0; i < steps; i++)
                    {
                        switch (temp)
                        {
                            case 'A':
                                temp = 'Z';
                                break;
                            case 'a':
                                temp = 'z';
                                break;
                            case '0':
                                temp = '9';
                                break;
                            default:
                                temp--;
                                break;
                        }
                    }

                    result += temp;
                }
            }

            return result;
        }

        private bool isAlphaNumeric(string input)
        {
            Regex r = new Regex("^[a-zA-Z0-9]*$");

            return r.IsMatch(input);
        }
    }

}
